package event;

import java.io.Serializable;
import java.util.Calendar;

/**
 * This class representing the real event object
 * @author Argo, carpediem, leone1386
 *
 */
public class Event implements Serializable {
	
	private static final long serialVersionUID = -7210399512042154310L;
	
	protected long id;
	
	public static final String EVENT_SHUTDOWN 	= "shutdown";
	public static final String EVENT_RESULT		= "result";
	public static final String EVENT_UPDATECONF	= "updateconf";
	public static final String EVENT_DEQUEUE 	= "dequeue";
	
    protected String type;
    protected String content;
        
    /**
    * Builder with four parameters
    * @param anId
    * @param aType
    * @param aContent
    */
    public Event(long anId, String aType, String aContent)
    {
    	super();
    	id = anId;
        if(aType!=null)
        	type = aType;
        else
            type = new String();
                
        if(aContent!=null)
        	content = aContent;
        else
            content = new String();
     }
        
        /**
         * 
         * @return ID of Event object
         */
        public long getId()
        {
                return this.id;
        }
        
        /**
         * 
         * @return type of Event object
         */
        public String getType()
        {
                return this.type;
        }
        
        /**
         * 
         * @return content of Event object
         */
        public String getContent()
        {
                return this.content;
        }
        
     /**
     * 
     * @param newId
     */
     public void setId(long newId)
     {
        	id = newId;
     }
        
    /**
    * Check if the type is present into a possible event pool
    * @param newType
    */
    public void setType(String newType)
        {
		if(Event.isValidEvent(newType))
			this.type = newType;
		else
			System.out.println(" >> EVENT: Cannot set new Type. Please, check the available .");
        }
    
    public static final String[] possibleEvents = {Event.EVENT_SHUTDOWN, Event.EVENT_RESULT, Event.EVENT_DEQUEUE, Event.EVENT_UPDATECONF};
	
	
	public static final boolean isValidEvent(String type)
	{
		for(int i=0; i<Event.possibleEvents.length; i++)
		{
			if(Event.possibleEvents[i].equalsIgnoreCase(type) == true)
				return true;
		}
		return false;
	}
	
	/**
	 * Builder with two parameters
	 * @param aType 
	 * @param aContent
	 */
	
	public Event(String aType, String aContent)
	{
		super();
		
		id = Long.parseLong(String.valueOf(Thread.currentThread().getId()) + 
				String.valueOf(Calendar.getInstance().getTimeInMillis()));
		if(aType!=null)
	        type = aType;
	else
	        type = new String();
	
	if(aContent!=null)
	        content = aContent;
	else
	        content = new String();
	}
	

	/**
     * 
     * @param newContent
     * @exception Exception
     */

	
	public void setContent(String newContent)
	{
		try{
			// Controllo il formato della nuova String
			String[] tempList = newContent.split(",");
			for(int i=0; i<tempList.length; i++)
	    	{
	    		tempList[i].split(":");
	    	}
	    	
			content = new String(newContent);
		} catch(Exception e)
		{
			System.out.println(" >> EVENT: Cannot set new Content. Please, check the string format.");
		}
	}
	
	/**
	 * This method return a formatted string
	 * @return a string in the format EVENT ID:TYPE:CONTENT
	 */
	public String toString()
	{
		return String.format("\n\tEVENT ID:\t%d%n\tTYPE:\t\t%s\n\tCONTENT:\t%s", 
				this.id, 
				this.type, 
				this.content); 
	}
}